using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class FriendProfileConfig : ScriptableObject
{
	public class FriendProfileWrapper
	{
		public CareerScoreDAO careerScore;

		public string playerName { get; set; }

		public string pictureUrl { get; protected set; }

		public string fbIdForApp { get; protected set; }

		public int starsCollected
		{
			get
			{
				int num = 0;
				if (careerScore.stageScores != null)
				{
					foreach (CareerScoreDAO.StageScore stageScore in careerScore.stageScores)
					{
						num += stageScore.stars;
					}
				}
				return num;
			}
		}

		public int maxStageReached
		{
			get
			{
				return careerScore.maxStageReached;
			}
		}

		public FriendProfileWrapper(FriendsProfiles.FriendProfileData friendData)
		{
			playerName = friendData.name;
			pictureUrl = friendData.picture.data.url;
			fbIdForApp = friendData.id;
			string keyForFile = instance.GetKeyForFile(ProfileFileHandle.CareerStage);
			string fileDataForKey = GetFileDataForKey(friendData.files, keyForFile);
			if (fileDataForKey == string.Empty || !ProtoIO.LoadFromBase64String<CareerScoreDAO>(fileDataForKey, out careerScore))
			{
				careerScore = new CareerScoreDAO();
			}
		}

		public FriendProfileWrapper(CareerScoreDAO careerScore, string name, string id, string url)
		{
			playerName = name;
			pictureUrl = url;
			fbIdForApp = id;
			this.careerScore = careerScore;
		}

		private string GetFileDataForKey(List<CloudSyncData.CloudSyncFile> files, string key)
		{
			foreach (CloudSyncData.CloudSyncFile file in files)
			{
				if (file.key == key)
				{
					return file.data;
				}
			}
			return string.Empty;
		}

		public CareerScoreDAO.StageScore GetStageScore(string name)
		{
			if (careerScore.stageScores == null)
			{
				careerScore.stageScores = new List<CareerScoreDAO.StageScore>();
			}
			foreach (CareerScoreDAO.StageScore stageScore2 in careerScore.stageScores)
			{
				if (stageScore2.name == name)
				{
					return stageScore2;
				}
			}
			CareerScoreDAO.StageScore stageScore = new CareerScoreDAO.StageScore();
			stageScore.name = name;
			careerScore.stageScores.Add(stageScore);
			return stageScore;
		}
	}

	public enum ProfileFileHandle
	{
		CareerStage = 0
	}

	[Serializable]
	public class ProfileFile
	{
		public ProfileFileHandle handle;

		public string fileKey;
	}

	private static FriendProfileConfig instance_;

	public List<ProfileFile> profileFiles = new List<ProfileFile>();

	private List<string> filenames = new List<string>();

	public static FriendProfileConfig instance
	{
		get
		{
			if (instance_ == null)
			{
				instance_ = Resources.Load("GGServerAssets/FriendProfileConfig", typeof(FriendProfileConfig)) as FriendProfileConfig;
			}
			return instance_;
		}
	}

	public List<string> GetProfileFiles()
	{
		filenames.Clear();
		foreach (ProfileFile profileFile in profileFiles)
		{
			filenames.Add(profileFile.fileKey);
		}
		return filenames;
	}

	public string GetKeyForFile(ProfileFileHandle handle)
	{
		foreach (ProfileFile profileFile in profileFiles)
		{
			if (profileFile.handle == handle)
			{
				return profileFile.fileKey;
			}
		}
		return string.Empty;
	}
}
